package net.pws.common.security.spi.resource;

import net.sf.ehcache.Ehcache;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

public class CachedResourceManagerFactory implements
                                         FactoryBean<ICachedResourceManager>,
                                         InitializingBean {
    
    private static CachedResourceManagerFactory instance;
    
    public static CachedResourceManagerFactory getInstance() {
        return instance;
    }
    
    private ICachedResourceManager mgr;
    
    private ResourceEhCacheManager cacheManager;
    
    private IResourceRepository resourceRepository;
    
    public void setEhcache(Ehcache ehcache) {
        this.cacheManager = new ResourceEhCacheManager(ehcache);
    }
    
    public void setResourceRepository(IResourceRepository resourceRepository) {
        this.resourceRepository = resourceRepository;
    }
    
    public ICachedResourceManager getObject() throws Exception {
        return mgr;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public Class getObjectType() {
        return DefaultCachedResourceManager.class;
    }
    
    public boolean isSingleton() {
        return true;
    }
    
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(cacheManager);
        Assert.notNull(resourceRepository);
        mgr = new DefaultCachedResourceManager(cacheManager, resourceRepository);
        instance = this;
    }
    
}
